﻿using SIG.DAL.Dapper.Model;
using SIG.DAL.Dapper.Repositories;
using SIG.Infrastructure.Configs;
using SIG.Infrastructure.Helper;
using SIG.InputModel.Front.Emails;
using SIG.Resources.Front;
using SIG.ViewModel;
using System;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    public class ContactController : BaseController
    {
        Infrastructure.Email.IEmailService _emailService;
        EmailTemplateRepository _templateService;
        EmailRepository _emailListService;
        EmailAccountRepository _emailAccountRepository;
        private readonly PageMetaRepository _pageMetaService;
        public ContactController(Infrastructure.Email.IEmailService emailService)
        {
            _emailService = emailService;
            _templateService = new EmailTemplateRepository();
            _emailListService = new EmailRepository();
            _emailAccountRepository = new EmailAccountRepository();
            _pageMetaService = new PageMetaRepository();
        }
        // GET: /Contact/

        public ActionResult Index()
        {

            var vm = new EmailIM();

            var url = Request.RawUrl;
            ViewBag.PageMeta = _pageMetaService.GetPageMeta(ModelType.MENU, url);

            return View(vm);
        }

        [HttpPost]
        public JsonResult SendEmail(EmailIM vm)
        {
            var template = _templateService.GetEmailTemplateByTemplateNo("T003");
            if (template == null)
            {
                AR.Setfailure(string.Format(Messages.NoEmailTemplate, "T003"));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }


            vm.Subject = SettingsManager.Site.SiteName + "联系邮件";
            var emailBody = _templateService.ReplaceTemplate(template.Body);
            emailBody = emailBody.Replace("{Name}", vm.Name);
            emailBody = emailBody.Replace("{Phone}", vm.Phone);
            emailBody = emailBody.Replace("{Email}", vm.Email);
            emailBody = emailBody.Replace("{Message}", vm.Body);

            var emailAccount = _emailAccountRepository.GetById(template.EmailAccountId);

            try
            {
                _emailService.SendMail(vm.Name, SettingsManager.Contact.MailTo, SettingsManager.Contact.MailTo, SettingsManager.Contact.MailCC,
                 vm.Subject, emailBody, emailAccount.Smtpserver, emailAccount.Email, SettingsManager.Site.SiteName,
                 emailAccount.UserName, EncryptionHelper.Decrypt(emailAccount.Password), (int)emailAccount.Port, emailAccount.EnableSsl);


                Email email = new Email
                {
                    Body = emailBody,
                    Subject = vm.Subject,
                    MailTo = vm.Email,
                    MailCc = SettingsManager.Contact.MailCC,
                    Active = true,
                    CreatedBy = vm.Name,
                    CreatedDate = DateTime.Now
                };

                _emailListService.Create(email);

                AR.SetSuccess(Messages.EmailSentSuccess);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            catch (Exception er)
            {
                AR.Setfailure(er.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

        }

        public PartialViewResult ContactProduct()
        {
            ContactIM im = new ContactIM();
            return PartialView("_ContactProduct", im);
        }

        [HttpPost]
        public JsonResult ContactProduct(ContactIM vm)
        {
            var template = _templateService.GetEmailTemplateByTemplateNo("T003");
            if (template == null)
            {
                AR.Setfailure(string.Format(Messages.NoEmailTemplate, "T003"));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }


            var subject = "[HSG]Product page contact form";

            var emailBody = _templateService.ReplaceTemplate(template.Body);

            emailBody = emailBody.Replace("{Name}", vm.Name);
            emailBody = emailBody.Replace("{Country}", vm.Country);
            emailBody = emailBody.Replace("{Company}", vm.Company);
            emailBody = emailBody.Replace("{Email}", vm.Email);
            emailBody = emailBody.Replace("{Phone}", vm.Phone);
            emailBody = emailBody.Replace("{Message}", vm.Message);

            var emailAccount = _emailAccountRepository.GetById(template.EmailAccountId);

            try
            {
                _emailService.SendMail(vm.Name, vm.Email, SettingsManager.Contact.MailTo, SettingsManager.Contact.MailCC,
                 subject, emailBody, emailAccount.Smtpserver, emailAccount.Email, SettingsManager.Site.SiteName,
                 emailAccount.UserName, EncryptionHelper.Decrypt(emailAccount.Password), emailAccount.Port, emailAccount.EnableSsl);


                Email email = new Email
                {
                    Body = emailBody,
                    Subject = subject,
                    MailTo = vm.Email,
                    MailCc = SettingsManager.Contact.MailCC,
                    Active = true,
                    CreatedBy = vm.Name,
                    CreatedDate = DateTime.Now
                };

                _emailListService.Create(email);

                AR.SetSuccess(Messages.EmailSentSuccess);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            catch (Exception er)
            {
                AR.Setfailure(er.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

        }
    }
}